﻿$(function () {
    $("#btnSave").click(function () {
        if(validate()){            
            save();
        }
    });
});

//验证表单
function validate() {
    var oldPassword = $("#OldPassword").val();   
    if (oldPassword==""||oldPassword==null) {
        str = '旧密码不能为空';
        layer.tips(str, "#btnSave");
        $("#OldPassword").focus();
        return false;
    }
    var newPassword = $("#NewPassword").val();    
    if (newPassword == "" || newPassword == null) {
        str = '新密码不能为空';
        layer.tips(str, "#btnSave");
        $("#NewPassword").focus();
        return false;
    }
    var newPassword1 = $("#NewPassword1").val();
    if (newPassword1 == "" || newPassword1 == null) {
        str = '确认新密码不能为空';
        layer.tips(str, "#btnSave");
        $("#NewPassword1").focus();
        return false;
    }
    if (newPassword != newPassword1) {
        str = '两次输入的密码不一样';
        layer.tips(str, "#btnSave");
        $("#NewPassword1").focus();
        return false;
    }
    return true;
}
//保存
function save() {
    //传递两个参数，注意参数名称必须与controller中一样
    var postData = {
        "oldPsw": $("#OldPassword").val(),
        "item":{
            "Id": $("#Id").val(),
            "Password": $("#NewPassword").val()
        }
    };
    $.ajax({
        type: "Post",
        url: "/Sys/User/ModifyPsw",
        data: postData,
        success: function (data) {
            if (data.result == "Success") {
                alert("更改密码成功，请重新登录。")
                window.location.href = "/Home/Login";
            } else {
                layer.tips(data.message, "#btnSave");
            };
        }
    });
};
